package com.gxa.hualianeshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.hualianeshop.common.base.RedisKeys;
import com.gxa.hualianeshop.common.base.ResultCode;
import com.gxa.hualianeshop.common.exception.BizException;
import com.gxa.hualianeshop.entity.dto.UserLoginDto;
import com.gxa.hualianeshop.entity.pojo.Spu;
import com.gxa.hualianeshop.entity.pojo.User;
import com.gxa.hualianeshop.entity.vo.UserVO;
import com.gxa.hualianeshop.mapper.SpuMapper;
import com.gxa.hualianeshop.mapper.UserMapper;
import com.gxa.hualianeshop.service.SpuService;
import com.gxa.hualianeshop.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gxa
 * @since 2023-07-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    @Override
    public String sendSMS(String phone) {
        // 生成验证码
        StringBuilder builder = new StringBuilder();
        for (int i=0;i<6;i++){
            builder.append(new Random().nextInt(9)+1);
        }

        // 存入redis , 设定15分钟有效时间
        redisTemplate.opsForValue().set(RedisKeys.SMS_KEY+phone,builder.toString(),15, TimeUnit.MINUTES);
        return builder.toString();
    }

    @Override
    public String getByPhoneAndCode(UserLoginDto userLoginDto) {
        // 从dto对象中解析手机号和验证码
        String phone = userLoginDto.getPhone();
        String code = userLoginDto.getCode();

        Object redisCode = redisTemplate.opsForValue().get(RedisKeys.SMS_KEY + phone);
        if(redisCode==null){
            // 手机号填写不对, 验证码过期
            throw new BizException(ResultCode.LOGIN_PHONE_ERROR);
        }

        // 比较用户提交的code和redis中的code
        if(!code.equals(redisCode)){
            // 手机号填写不对, 验证码过期
            throw new BizException(ResultCode.CODE_ERROR);
        }

        // 校验完成, 给用户生成token(uuid)
        String uuidToken  = UUID.randomUUID().toString().replaceAll("-","");

        // 存入redis:  uuid+salt
        String key ="user:"+uuidToken+RedisKeys.TOKEN_SALT;

        // 获取用户信息
        LambdaQueryWrapper<User> userLambdaQueryWrapper = Wrappers.lambdaQuery(User.class);
        userLambdaQueryWrapper.eq(User::getPhone,phone);
        User one = this.getOne(userLambdaQueryWrapper);

        redisTemplate.opsForValue().set(key,one,15,TimeUnit.MINUTES);
        // 返回token给前端
        return uuidToken;
    }

    @Override
    public UserVO getUserVOByToken(String token) {
        // 组装redis中的key值
        String key = "user:"+token+RedisKeys.TOKEN_SALT;
        Object o = redisTemplate.opsForValue().get(key);
        if(o == null){
            throw new BizException(ResultCode.FETCH_ACCESSTOKEN_FAIL);
        }

//        获取User信息拷贝到UserVO
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(o,userVO);

        return userVO;
    }
}
